package com.gc.baselib.storage;

import android.content.Context;
import android.os.Parcelable;
import android.text.TextUtils;

import com.gc.baselib.utils.GsonUtil;
import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author admin
 * @Date 2022/6/9 23:14
 * @package： com.zk.baselib.storage
 * @decrpition
 */
public class MMKVStorage {
    private MMKV mmkv;
    public static MMKVStorage getInstance(){
        return MmkvHolder.INSTANCE;
    }
    private MMKVStorage(){
    }
    private static class MmkvHolder{
        private static final MMKVStorage INSTANCE = new MMKVStorage();
    }
    public  void init(Context context) {
        MMKV.initialize(context.getApplicationContext());
        mmkv = MMKV.defaultMMKV();
    }
    public MMKV getMmkv(){
        if(mmkv==null){
            throw new IllegalStateException("You should call init() first.");
        }
        return mmkv;
    }
    public void putString(String key,String value ){
        getMmkv().encode(key,value);
    }
    public void putInt(String key,int value ){
        getMmkv().encode(key,value);
    }
    public void putBoolean(String key,boolean value ){
        getMmkv().encode(key,value);
    }
    public void putFloat(String key,float value ){
        getMmkv().encode(key,value);
    }
    public void putLong(String key,long value ){
        getMmkv().encode(key,value);
    }
    public void putDouble(String key,double value ){
        getMmkv().encode(key,value);
    }
    public void putByteArray(String key,byte[] value ){
        getMmkv().encode(key,value);
    }
    public void putParcelable(String key, Parcelable value ){
        getMmkv().encode(key,value);
    }
    public void putSetString(String key, Set<String> value ){
        getMmkv().encode(key,value);
    }


    /**
     * 存入map集合
     *
     * @param key 标识
     * @param map 数据集合
     */
    public   void putMap(String key, Map<String, Object> map)
    {
        Gson gson = new Gson();
        JSONArray mJsonArray = new JSONArray();
        JSONObject object = null;
        try
        {
            object = new JSONObject(gson.toJson(map));
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        mJsonArray.put(object);
        mmkv.encode(key, mJsonArray.toString());
    }
    public void putObject(String key, Object object)
    {
        mmkv.encode(key, GsonUtil.toJson(object));
    }
    public double getDouble(String key) {
        return mmkv.decodeDouble(key, 0.00);
    }

    public long getLong(String key) {
        return mmkv.decodeLong(key, 0L);
    }
    public int getInt(String key) {
        return mmkv.decodeInt(key, 0);
    }

    public boolean getBoolean(String key) {
        return mmkv.decodeBool(key, false);
    }
    public boolean getBoolean(String key,boolean defaultValue) {
        return mmkv.decodeBool(key, defaultValue);
    }

    public float getFloat(String key) {
        return mmkv.decodeFloat(key, 0F);
    }

    public byte[] getBytes(String key) {
        return mmkv.decodeBytes(key);
    }

    public String getString(String key, String defaultValue) {
        return mmkv.decodeString(key, defaultValue);
    }

    public Set<String> getStringSet(String key) {
        return mmkv.decodeStringSet(key, Collections.<String>emptySet());
    }

    public <T extends Parcelable> T getParcelable(String key, Class<T> tClass) {
        return mmkv.decodeParcelable(key, tClass);
    }

    /**
     * 扩展MMKV类使其支持对象存储
     *
     */

    public   <T> T getObject(String key, Class<T> cls)
    {
        String str = mmkv.decodeString(key, null);
        if (!TextUtils.isEmpty(str))
        {
            return GsonUtil.parseJson(str,cls);
        }
        else
        {
            return null;
        }
    }
    /**
     * 获取map集合
     *
     * @param key 标识
     */
    public   Map<String, String> getMap(String key)
    {
        Map<String, String> itemMap = new HashMap<>();
        String result = mmkv.decodeString(key, "");
        try
        {
            JSONArray array = new JSONArray(result);
            for (int i = 0; i < array.length(); i++)
            {
                JSONObject itemObject = array.getJSONObject(i);
                JSONArray names = itemObject.names();
                if (names != null)
                {
                    for (int j = 0; j < names.length(); j++)
                    {
                        String name = names.getString(j);
                        String value = itemObject.getString(name);
                        itemMap.put(name, value);
                    }
                }
            }
        }
        catch (JSONException e)
        {

        }
        return itemMap;
    }

    public void remove(String key) {
        mmkv.removeValueForKey(key);
    }

    public void clear() {
        mmkv.clearAll();
    }


}
